package com.bank.dao;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bank.pojo.BankUserinfo;

@Component("bankUserDao")
@Transactional
public class BankUserDao extends BaseDao{
	@SuppressWarnings("unchecked")
	public boolean addUser(BankUserinfo user){
		List<BankUserinfo> list=getSession().createQuery("from BankUserinfo").list();
		for(BankUserinfo bui:list){
			if(bui.getUsersName().equals(user.getUsersName())){
				return false;
			}
			if(bui.getUsersDocumentnumber().equals(user.getUsersDocumentnumber())){
				return false;
			}
		}
		getSession().save(user);
		return true;
	}
	
	public BankUserinfo getUserByDoc(String docNum){
		BankUserinfo user=(BankUserinfo)getSession().createQuery("from BankUserinfo where usersDocumentnumber=?")
							.setString(0, docNum).uniqueResult();
		return user;
	}
	
	public boolean checkUserAccount(String docNum){
		BankUserinfo user=(BankUserinfo)getSession().createQuery("from BankUserinfo where usersDocumentnumber=?")
				.setString(0,docNum).uniqueResult();
		if(null!=user){
			return true;
		}
		return false;
	}
	
	public int isUserExist(String docNum,String phoneNum){
		BankUserinfo user=(BankUserinfo)getSession().createQuery("from BankUserinfo where usersDocumentnumber=?")
				.setString(0,docNum).uniqueResult();
		if(null==user){
			return 1;//docNum ERROR
		}
		if(!user.getUsersTelephone().equals(phoneNum)){
			return 2;//phoneNum ERROR
		}
		return 0;
	}
	
}
